<?
class ManagePromotionHotel extends Module
{
    function ManagePromotionHotel($row)
    {
        Module::Module($row);


        $hotel_ids = '';


        switch (Url::get('cmd')) {
            case 'gpm':


                break;
            case 'edit':
                if (User::have_permit('hotel_edit')) {
                    CGlobal::$website_title = "Chỉnh sửa chính sách khách sạn";

                    require_once "forms/ManagePromotionHotelEdit.php";
                    $this->add_form(new ManagePromotionHotelEditForm());
                } else {
                    Url::redirect_current();
                }

                break;
            case 'add':
                if (User::have_permit('hotel_add')) {
                    CGlobal::$website_title = "Thêm chính sách khách sạn";

                    require_once "forms/ManagePromotionHotelAdd.php";
                    $this->add_form(new ManagePromotionHotelAddForm);
                } else {
                    Url::redirect_current();
                }
                break;

            case 'delete':
                $id = Url::getInt('id', 0);

                if ($id) {
                    $PromotionHotel = Db_Promotion::getPromotionHotelById($id);

                    if ($PromotionHotel && User::have_permit("hotel_delete")) {
                        Db_Promotion::delById($id);
                    }
                }

                $href = Url::get('href');

                if ($href) {
                    Url::redirect_url($href);
                } else {
                    Url::redirect_current(array('hotel_id'));
                }
                break;

            case 'show':
                $id = Url::getInt('id', 0);

                if ($id) {
                    $hotel = Db_Promotion::getPromotionHotelById($id);
                    if ($hotel && $hotel['status'] != 1 && User::have_permit("hotel_edit")) {
                        $d = Db_Promotion::getPromotionFilterById($hotel['type']);
                        foreach ($d as &$e) {
                            $e['num_hotel'] += 1;
                            DB::update('promotion_filter', $e, ' id =' . $e['id'], '');
                            break;
                        }
                        $ca = Db_Hotel::getHotelById($hotel['hotel_id']);
                        $dd = Db_Location::getLocationById($ca['city_id']);
                        $ed = $dd['num_promotion_hotel']+1;
                        Db_Location::update(array('num_promotion_hotel' => $ed), $ca['city_id']);
                        if ($dd["id_citiesfather"] != 0){
                            $dd = Db_Location::getLocationById($dd["id_citiesfather"]);
                            $ed = $dd['num_promotion_hotel'] + 1;
                            Db_Location::update(array('num_promotion_hotel' => $ed), $dd["id_citiesfather"]);
                        }

                        DB::update_id('promotion_hotel', array("status" => 1), $id, __FILE__ . ' line ' . __LINE__);

                    }
                }

                $href = Url::get('href');
                if ($href) {
                    Url::redirect_url($href);
                } else {
                    Url::redirect_current(array('hotel_id'));
                }
                break;

            case 'hide':
                $id = Url::getInt('id', 0);

                if ($id) {
                    $hotel = Db_Promotion::getPromotionHotelById($id);

                    if ($hotel && $hotel['status'] != 2 && User::have_permit("hotel_edit")) {
                        $d = Db_Promotion::getPromotionFilterById($hotel['type']);
                        foreach ($d as &$e) {
                            $e['num_hotel'] -= 1;
                            DB::update('promotion_filter', $e, ' id =' . $e['id'], '');
                            break;
                        }
                        $ca = Db_Hotel::getHotelById($hotel['hotel_id']);
                        $dd = Db_Location::getLocationById($ca['city_id']);
                        $ed = $dd['num_promotion_hotel']-1;
                        Db_Location::update(array('num_promotion_hotel' => $ed), $ca['city_id']);
                        if ($dd["id_citiesfather"] != 0){
                            $dd = Db_Location::getLocationById($dd["id_citiesfather"]);
                            $ed = $dd['num_promotion_hotel'] - 1;
                            Db_Location::update(array('num_promotion_hotel' => $ed), $dd["id_citiesfather"]);
                        }

                        DB::update_id('promotion_hotel', array("status" => 2), $id, __FILE__ . ' line ' . __LINE__);
                    }
                }

                $href = Url::get('href');
                if ($href) {
                    Url::redirect_url($href);
                } else {
                    Url::redirect_current(array('hotel_id'));
                }
                break;
            case 'restore':
                $id = Url::getInt('id', 0);

                if ($id) {
                    $hotel = Db_Promotion::getPromotionHotelById($id);
                    if ($hotel && $hotel['status'] != 1 && User::have_permit("hotel_edit")) {
                        $d = Db_Promotion::getPromotionFilterById($hotel['type']);
                        foreach ($d as &$e) {
                            $e['num_hotel'] += 1;
                            DB::update('promotion_filter', $e, ' id =' . $e['id'], '');
                            break;
                        }

                        $ca = Db_Hotel::getHotelById($hotel['hotel_id']);
                        $dd = Db_Location::getLocationById($ca['city_id']);
                        $ed = $dd['num_promotion_hotel']+1;
                        Db_Location::update(array('num_promotion_hotel' => $ed), $ca['city_id']);
                        if ($dd["id_citiesfather"] != 0){
                            $dd = Db_Location::getLocationById($dd["id_citiesfather"]);
                            $ed = $dd['num_promotion_hotel'] + 1;
                            Db_Location::update(array('num_promotion_hotel' => $ed), $dd["id_citiesfather"]);
                        }

                        DB::update_id('promotion_hotel', array("status" => 1), $id, __FILE__ . ' line ' . __LINE__);
                    }
                }

                $href = Url::get('href');
                if ($href) {
                    Url::redirect_url($href);
                } else {
                    Url::redirect_current(array('hotel_id'));
                }

                break;
            default:
                CGlobal::$website_title = "Quản trị PromotionHotel Hotel";

                require_once "forms/ManagePromotionHotel.php";
                $this->add_form(new ManagePromotionHotelForm());

                break;
        }
    }

    static function permission()
    {
        return array(
            //'hotel_admin' 	=> "Quản lý Khách sạn",
            'hotel_delete' => "Xóa Hotel",
            'hotel_add' => "Thêm Hotel",
            'hotel_edit' => "Sửa Hotel",
            'hotel_list' => "List Hotel"
        );
    }
}

?>